package com.wang.cloud.manage.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.cloud.common.mysql.utils.PageUtils;
import com.wang.cloud.manage.convert.PlatformUserConvertor;
import com.wang.cloud.manage.domain.co.PlatformUserPageCo;
import com.wang.cloud.manage.domain.entity.PlatformUserEntity;
import com.wang.cloud.manage.domain.vo.PlatformUserPageVo;
import com.wang.cloud.manage.domain.vo.PlatformUserVo;
import com.wang.cloud.manage.domain.vo.SysUserPageVo;
import com.wang.cloud.manage.mapper.PlatformUserMapper;
import com.wang.cloud.manage.service.IPlatformUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统平台用户信息表 服务实现类
 * </p>
 *
 * @author wy
 * @since 2024-07-16
 */
@Service
public class PlatformUserServiceImpl extends ServiceImpl<PlatformUserMapper, PlatformUserEntity> implements IPlatformUserService {

    @Resource
    private PlatformUserConvertor convertor;

    @Override
    public IPage<SysUserPageVo> sysPage(PlatformUserPageCo pageCo) {
        return this.lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(pageCo.getPlatformId()), PlatformUserEntity::getPlatformId, pageCo.getPlatformId())
                .eq(ObjectUtil.isNotEmpty(pageCo.getUserName()), PlatformUserEntity::getUserName, pageCo.getUserName())
                .eq(ObjectUtil.isNotEmpty(pageCo.getPhone()), PlatformUserEntity::getPhone, pageCo.getPhone())
                .eq(ObjectUtil.isNotEmpty(pageCo.getNickName()), PlatformUserEntity::getNickName, pageCo.getNickName())
                .eq(ObjectUtil.isNotEmpty(pageCo.getGender()), PlatformUserEntity::getGender, pageCo.getGender())
                .eq(ObjectUtil.isNotEmpty(pageCo.getStatus()), PlatformUserEntity::getStatus, pageCo.getStatus())
                .select(PlatformUserEntity::getId
                        , PlatformUserEntity::getPlatformId
                        , PlatformUserEntity::getUserName
                        , PlatformUserEntity::getPhone
                        , PlatformUserEntity::getPassword
                        , PlatformUserEntity::getNickName
                        , PlatformUserEntity::getUserType
                        , PlatformUserEntity::getEmail
                        , PlatformUserEntity::getGender
                        , PlatformUserEntity::getAvatar
                        , PlatformUserEntity::getStatus
                        , PlatformUserEntity::getLoginIp
                        , PlatformUserEntity::getLoginDate
                        , PlatformUserEntity::getRemark
                        , PlatformUserEntity::getUnionId
                        , PlatformUserEntity::getOpenid
                )
                .page(PageUtils.getPage(pageCo))
                .convert(convertor::entityToSysPageVo);
    }

    @Override
    public IPage<PlatformUserPageVo> page(PlatformUserPageCo pageCo) {
        return this.lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(pageCo.getPlatformId()), PlatformUserEntity::getPlatformId, pageCo.getPlatformId())
                .eq(ObjectUtil.isNotEmpty(pageCo.getUserName()), PlatformUserEntity::getUserName, pageCo.getUserName())
                .eq(ObjectUtil.isNotEmpty(pageCo.getPhone()), PlatformUserEntity::getPhone, pageCo.getPhone())
                .eq(ObjectUtil.isNotEmpty(pageCo.getNickName()), PlatformUserEntity::getNickName, pageCo.getNickName())
                .eq(ObjectUtil.isNotEmpty(pageCo.getGender()), PlatformUserEntity::getGender, pageCo.getGender())
                .eq(ObjectUtil.isNotEmpty(pageCo.getStatus()), PlatformUserEntity::getStatus, pageCo.getStatus())
                .select(PlatformUserEntity::getId
                        , PlatformUserEntity::getPlatformId
                        , PlatformUserEntity::getUserName
                        , PlatformUserEntity::getPhone
                        , PlatformUserEntity::getPassword
                        , PlatformUserEntity::getNickName
                        , PlatformUserEntity::getUserType
                        , PlatformUserEntity::getEmail
                        , PlatformUserEntity::getGender
                        , PlatformUserEntity::getAvatar
                        , PlatformUserEntity::getStatus
                        , PlatformUserEntity::getLoginIp
                        , PlatformUserEntity::getLoginDate
                        , PlatformUserEntity::getRemark
                        , PlatformUserEntity::getUnionId
                        , PlatformUserEntity::getOpenid
                )
                .page(PageUtils.getPage(pageCo))
                .convert(convertor::entityToPageVo);
    }

    @Override
    public PlatformUserEntity getUserById(String platformId, Long id) {
        return this.lambdaQuery()
                .eq(PlatformUserEntity::getPlatformId, platformId)
                .eq(PlatformUserEntity::getId, id)
                .one();
    }

    @Override
    public PlatformUserEntity getUserByPhone(String platformId, String phone) {
        return this.lambdaQuery()
                .eq(PlatformUserEntity::getPlatformId, platformId)
                .eq(PlatformUserEntity::getPhone, phone)
                .one();
    }

    @Override
    public PlatformUserVo getInfo(Long id) {
        PlatformUserEntity entity = this.getById(id);
        return convertor.entityToVo(entity);
    }

    @Override
    public PlatformUserVo getInfo(String platformId, Long id) {
        PlatformUserEntity entity = this.lambdaQuery()
                .eq(PlatformUserEntity::getPlatformId, platformId)
                .eq(PlatformUserEntity::getId, id)
                .one();
        return convertor.entityToVo(entity);
    }

}
